# +--------------------------
# | User: zq                -
# | Version: python3.7      -
# | Time: 2020-03-12 13:51                
# +--------------------------

from threading import Thread
# 线程同步
from threading import Lock

total = 0
total_lock = Lock()  # 线程锁


# 一把锁,上完锁之后,只有释放了,下一个线程才难继续运行,不然拿不到


def add():
    total_lock.acquire()  # 拿到线程锁
    global total
    for i in range(1000000):
        total += 1
    total_lock.release()  # 释放线程锁


def desc():
    total_lock.acquire()  # 拿到线程锁
    global total
    for i in range(1000000):
        total -= 1
    total_lock.release()  # 释放线程锁


if __name__ == '__main__':
    add_thread = Thread(target=add)
    desc_thread = Thread(target=desc)

    add_thread.start()
    desc_thread.start()

    add_thread.join()
    desc_thread.join()

    print(total)
